﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using CoreShop.Model;
using CoreShop.Services;
using Sunny.UI;
using CoreShop.Library;
using System.Linq;
using SqlSugar.Extensions;
using System.Diagnostics.SymbolStore;
using static CoreShop.Library.GlobalEnumVars;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;
using CoreShop.Model.FromBody;
using System.Threading;

namespace CoreShop.App
{
    public partial class frmDataClear : UIForm
    {
        public frmDataClear()
        {
            InitializeComponent();
        }

        private void frmTableOpen_Load(object sender, EventArgs e)
        {

        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            bool returnValue = false;
            if (!ShowAskDialog("所选的数据清除后将无法恢复，确定要清除吗？")) return;
            var corecmsexchangeSvr = new corecmsexchangeServices();
            var exchange = corecmsexchangeSvr.GetDataById(AppConstVars.sysUser.storeId);
            DateTime dateTime = DateTime.Now.AddDays(-99999);

            //returnValue = new corecmsemployeeServices().Delete(p => p.id > 0);          
            if (depRole.Checked) new corecmsuserdepositrulesServices().Delete(p => p.id > 0);
            if (user.Checked) new sysuserServices().Delete(p => p.id > 0);
            if (grade.Checked) new corecmsusergradeServices().Delete(p => p.id > 0);
            if (mallOrders.Checked) returnValue = new corecmsordermasterServices().Delete(p => p.id > 0);
            if (userType.Checked)
            {
                returnValue = new corecmsusercategoryServices().Delete(p => p.id > 0);
                returnValue = new corecmsuserServices().Delete(p => p.id > 0);
                returnValue = new corecmsuserbalanceServices().Delete(p => p.id > 0);
                returnValue = new corecmsuserpointServices().Delete(p => p.id > 0);
            }
            if (food.Checked)
            {
                returnValue = new corecmsfoodServices().Delete(p => p.id > 0);
                returnValue = new corecmsfooddetailtaskServices().Delete(p => p.id > 0);
                returnValue = new corecmsfoodpackServices().Delete(p => p.id > 0);
                returnValue = new corecmsfoodpackdetailServices().Delete(p => p.id > 0);
                returnValue = new corecmsfoodsoldoutServices().Delete(p => p.id > 0);
                returnValue = new corecmsfoodspecServices().Delete(p => p.id > 0);
                returnValue = new corecmsfoodtotalServices().Delete(p => p.id > 0);
                returnValue = new corecmsfooddetailfeedServices().Delete(p => p.id > 0);
                returnValue = new corecmsfoodpriceServices().Delete(p => p.pdtSpuSpecId > 0);
                returnValue = new corecmsfoodbarcodesServices().Delete(p => p.spuId > 0);
                returnValue = new corecmsfoodunitServices().Delete(p => p.uomId > 0);
                returnValue = new corecmschgpriceServices().Delete(p => p.chgpriceLineId > 0);
                exchange.foodTime = dateTime;
                exchange.foodSpecTime = dateTime;
                exchange.foodUnitTime = dateTime;
                exchange.activityTime = dateTime;
                exchange.chgPriceTime = dateTime;
                exchange.branchPriceTime = dateTime;
            }
            if (employee.Checked)
            {
                new corecmsemployeeServices().Delete(p => p.id > 0);
                exchange.employeeTime = dateTime;
            }
            if (foodCategory.Checked)
            {
                new corecmsfoodcategoryServices().Delete(p => p.id > 0);
                exchange.foodCategoryTime = dateTime;
            }
            if (desk.Checked) new corecmsfooddeskServices().Delete(p => p.id > 0);
            if (deskType.Checked) new corecmsfooddeskcategoryServices().Delete(p => p.id > 0);
            if (feedType.Checked) new corecmsfoodfeedcategoryServices().Delete(p => p.id > 0);
            if (feed.Checked) new corecmsfoodfeedServices().Delete(p => p.id > 0);
            if (taskType.Checked)
            {
                returnValue = new corecmsfoodtaskcategoryServices().Delete(p => p.id > 0);
                returnValue = new corecmsfooddetailtaskServices().Delete(p => p.id > 0);
            }
            if (task.Checked)
            {
                returnValue = new corecmsfoodtaskServices().Delete(p => p.id > 0);
                returnValue = new corecmsfooddetailtaskServices().Delete(p => p.id > 0);
            }
            if (foodTask.Checked) new corecmsfooddetailtaskServices().Delete(p => p.id > 0);
            if (foodTag.Checked) new corecmsfoodtagServices().Delete(p => p.id > 0);
            if (payment.Checked)  new corecmspaymentsService().Delete(p => p.id > 11);
            DateTime date = DateTime.Now.AddDays(3650);  
            if (someBillData.Checked) date = startDate.Value;

            if (allBillData.Checked)
            {
                await new corecmsprinttasklogServices().InsertAsync("清理销售数据");
                returnValue = new corecmsfoodorderclassServices().Delete(p => p.starDate < date);
                returnValue = new corecmsfoodorderingmasterServices().Delete(p => p.orderDate < date);
                returnValue = new corecmsfoodordermasterServices().Delete(p => p.orderDate < date);

                var db = new sysuserServices().GetInstance();
                string msql = "delete from corecmsfoodorderdetail where orderId not in (select orderId from corecmsfoodordermaster)";                
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodordering where orderId not in (select orderId from corecmsfoodorderingmaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodorderingfeed where orderId not in (select orderId from corecmsfoodorderingmaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodorderingtag where orderId not in (select orderId from corecmsfoodorderingmaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodorderingtask where orderId not in (select orderId from corecmsfoodorderingmaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodorderfeed where orderId not in (select orderId from corecmsfoodordermaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodorderpayment where orderId not in (select orderId from corecmsfoodordermaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodordertag where orderId not in (select orderId from corecmsfoodordermaster)";
                db.Ado.ExecuteCommand(msql);

                msql = "delete from corecmsfoodordertask where orderId not in (select orderId from corecmsfoodordermaster)";
                db.Ado.ExecuteCommand(msql);
            }
            if (activityData.Checked)
            {
                await new corecmsprinttasklogServices().InsertAsync("清理:"+ activityData.Text);
                returnValue = new activitygoodsServices().Delete(p => p.activityGoodsId > 0);
                returnValue = new activitygoodsexcludeServices().Delete(p => p.activityExcludeGoodsId > 0);
                returnValue = new activitylinerespServices().Delete(p => p.activityLineId > 0);
                returnValue = new activitymasterServices().Delete(p => p.activityInfoId > 0);
                returnValue = new activitysortServices().Delete(p => p.activityGoodsSortId > 0);
                exchange.activityTime = dateTime;
            }
            corecmsexchangeSvr.Updateable(exchange);
            MessageBox.Show("清理完成，需要重新启动前台系统");
            Environment.Exit(0);
        }

        private void allBaseData_CheckedChanged(object sender, EventArgs e)
        {
            foreach (Control item in pnlList.Controls)
            {
                if (item is UICheckBox)
                {
                    var MyCtl = item as UICheckBox;
                    MyCtl.Checked = allBaseData.Checked;
                    MyCtl.Enabled = true;
                }
            }
            pnlList.Enabled = !allBaseData.Checked;
        }

        private void foodCategory_CheckedChanged(object sender, EventArgs e)
        {
            var senderCtl = sender as UICheckBox;
            string mTag = senderCtl.Tag.ToString();
            foreach (Control item in pnlList.Controls)
            {
                if (item is UICheckBox)
                {
                    var MyCtl = item as UICheckBox;
                    var hTag = MyCtl.Tag.ToString();
                    if ((hTag.IndexOf(mTag) >= 0) & (mTag != hTag))
                    {
                        MyCtl.Enabled = !senderCtl.Checked;
                        if (senderCtl.Checked)
                        {
                            MyCtl.Checked = senderCtl.Checked;                            
                        }                        
                    }                    
                }
            }
        }

        private void btnDelExchange_Click(object sender, EventArgs e)
        {
            if (!ShowAskDialog("确定要清除本机所有下载记录吗？")) return;
            var corecmsexchangeSvr = new corecmsexchangeServices();
            var exchange = corecmsexchangeSvr.GetDataById(AppConstVars.sysUser.storeId);
            if (exchange != null)
            {
                corecmsexchange corecmsexchange = new corecmsexchange();
                corecmsexchange.storeId = AppConstVars.sysUser.storeId;
                corecmsexchangeSvr.Updateable(corecmsexchange);
                ShowInfoDialog("清除成功。");
            }
        }
    }
}
